package main

import (
	"fmt"
	"sync"
	"time"
	"your-project/sxhm"
)

var token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ3ZWNoYXRfcHVibGljMSIsInVzZXJJZCI6IjE4Nzc1ODcyNjU3MDczMTUyMDAiLCJuYW1lIjoi5b6u5L-h5YWs5LyX5Y-35pWj5a6iIiwib3JnSWQiOiIxODc3MzM4MjQ2MDkyODE2Mzg0IiwiZGV2aWNlQ29kZSI6IiIsInNhbGVTdGF0aW9uSWQiOiIxODc3MzM4MjQ3NzcwNTM3OTg0IiwiY3VzdG9tZXJJZCI6IjE4Nzc1ODcyNjU4NTgzMTAxNDQiLCJzb2NpYWxDdXN0b21lcklkIjoxOTQxNTEwNjI3OTI0MzAzODczLCJleHAiOjE3NTIzOTcxMDV9.uoLHO4BwwFo8IYUPHiA6VxjnVyTIC3VwvzmKAGNULT2sKr9J8NMh0xoGZIwM_Pt8-oPYgxpMhwxzjfT544J3r-sRtjzw-bvSSw2tNMzK2wrX0ecznXVx00xnyty1aachIGajeRMa3B68-gfRvI4pc_GDAMP8EmYXobwPR0EJSNw"

// https://ticket.sxhm.com/applet/bms/visitplan/visitPlan/list
var VisitPlanId = "1943958043171487744"
var staggeredReservationDailyId = "1943339471486771200"

// https://ticket.sxhm.com/applet/bms/goods/goods_item/get
var ticketId = "1877649618375811072"

func main() {

	// 使用WaitGroup等待所有请求完成
	var wg sync.WaitGroup

	// 限制最大并发数为5
	// wg.SetWorkerLimit(5)

	// 记录开始时间
	// start := time.Now()
	params := sxhm.GetOrderParams(token, VisitPlanId, staggeredReservationDailyId, ticketId)

	// 发起5次并发请求
	for i := 0; i < 3; i++ {
		wg.Add(1)
		go func(requestNum int) {
			defer wg.Done()
			// if err := sxhm.SendOrderRequest(params, requestNum); err != nil {
			// 	fmt.Printf("错误: %v\n", err)
			// }

			attempts := 0
			for {
				attempts++
				fmt.Printf("序列: %v , 尝试次: %v \n", requestNum, attempts)
				if err := sxhm.SendOrderRequest(params, requestNum); err != nil {
					fmt.Printf("错误: %v\n", err)
					time.Sleep(50 * time.Millisecond) // 等待 再重试
					continue
					// break
				}
				break
			}
		}(i + 1)
	}

	// 等待所有请求完成
	wg.Wait()

	// 计算总耗时
	// elapsed := time.Since(start)
	// fmt.Printf("5次并发请求总耗时: %s\n", elapsed)
}
